extends CenterContainer


onready var name_input = $PanelContainer/VBoxContainer/name/LineEdit
onready var weather_op = $PanelContainer/VBoxContainer/weather/OptionButton
onready var map_group_id_op = $PanelContainer/VBoxContainer/map_group_id/SearchSelect
onready var music_name_input = $PanelContainer/VBoxContainer/music_name/LineEdit
onready var quit_reset_pos_input = $PanelContainer/VBoxContainer/quit_reset_pos/TextEdit

var edit_data = null

var music_area_id


func set_music_area_id(music_area_id):
	self.music_area_id = music_area_id
	edit_data = DataSource.get_data_by_one(DataSource.map_area, music_area_id)
	
	weather_op.set_item_id(0, -1)
	
	map_group_id_op.set_data("选择世界地图位置", FlyMap.get_op_list())
		
	if edit_data:
		name_input.text = edit_data["mapName"]
		$PanelContainer/VBoxContainer/modalTitle.text = "编辑区域： %s" % edit_data["mapName"]
		weather_op.select(weather_op.get_item_index(edit_data["weather"]))
		map_group_id_op.set_value(edit_data["mapGroupId"])
		music_name_input.text = edit_data["musicName"]
		quit_reset_pos_input.text = edit_data.get("quitResetPos", "")
	else:
		
		pass
	



func _on_ok_button_up():
	var map_view = MainTool.getUILayer().get_ui("MapView")
	
	var position_info = map_view.get_select_position_info()
	
	if edit_data:
		read_form(edit_data)
		
		if !edit_data["mapName"]: 
			MainTool.toast_info("名称不能为空!")
			return
		
		DataSource.update_data(DataSource.map_area, edit_data)
		
	else:
		var data = {
			"id": music_area_id,
		}
		
		read_form(data)
		
		if !data["mapName"]: 
			MainTool.toast_info("名称不能为空!")
			return
		
		DataSource.add_data(DataSource.map_area, data)
	
	MainTool.toast_info("更新区域信息成功!")
	
	map_view.map_area_update()
	
	MainTool.getUILayer().pop_ui()

func read_form(dic):
	var map_view = MainTool.getUILayer().get_ui("MapView")
	
	var position_info = map_view.get_select_position_info()
	dic["mapName"] =  name_input.text
	dic["musicName"] =  music_name_input.text
	dic["mapGroupId"] =  map_group_id_op.get_value()
	dic["weather"] =  weather_op.get_selected_id()
	dic["mapFileId"] = position_info["map_register"]["id"]
	pass

func _on_cancel_button_up():
	MainTool.getUILayer().pop_ui()
